# -*- coding: utf-8 -*-
"""
Created on Fri May 20 10:16:01 2022

@author: Administrator
"""
import torch
import numpy
a = numpy.array([[1,  1, 1, 1, 0, 0],
                 [-1, 0, 1, 0, 1, 0],
                 [-1, 1, 0, 0, 0, 1]], dtype=float)
x = torch.from_numpy(a)
f = (x.shape[0] - 1) / x.shape[0]      # 方差调整系数
x_reducemean = x # - torch.mean(x, axis=0)
numerator = torch.matmul(x_reducemean.T, x_reducemean) / x.shape[0]
var_ = x.var(axis=0).reshape(x.shape[1], 1)
denominator = torch.sqrt(torch.matmul(var_, var_.T)) * f
corrcoef = numerator / denominator

cor_res = corrcoef.numpy() # torch转换到numpy


up = (1-1/3)*(0-1/3)+(0-1/3)*(1-1/3)+(0-1/3)*(0-1/3)
down1 = (1-1/3)**2+(0-1/3)**2+(0-1/3)**2